package com.telit.common.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @author zhouyu
 * @description 工具类
 */
public class StringUtils {

    /**
     * 判断userid里是否包含subStr
     *
     * @param userId
     * @param subStr
     * @return
     */
    public static boolean checkUserId(String userId, String subStr) {
        return userId.contains(convertStringToLowerCase(subStr)) || userId.contains(convertStringToUpperCase(subStr));
    }

    /**
     * 判断userid去除前缀是否等于subStr
     *
     * @param userId
     * @param subStr
     * @return
     */
    public static boolean checkEqualsUserId(String userId, String subStr) {
        String[] path = userId.split("_");
        if (path.length > 2) {
            return path[0].equals(convertStringToLowerCase(subStr)) || path[0].equals(convertStringToUpperCase(subStr));
        } else {
            return false;
        }
    }


    /**
     * 字符串转换为小写
     *
     * @param str
     * @return
     */
    private static String convertStringToLowerCase(String str) {
        char[] ch = str.toCharArray();
        StringBuilder sbf = new StringBuilder();
        for (char c : ch) {
            sbf.append(charToLowerCase(c));
        }
        return sbf.toString();
    }

    /**
     * 字符串转化为大写
     *
     * @param str
     * @return
     */
    private static String convertStringToUpperCase(String str) {
        char[] ch = str.toCharArray();
        StringBuilder sbf = new StringBuilder();
        for (char c : ch) {
            sbf.append(charToUpperCase(c));
        }
        return sbf.toString();
    }

    /**
     * 转大写
     **/
    private static char charToUpperCase(char ch) {
        if (ch <= 122 && ch >= 97) {
            ch -= 32;
        }
        return ch;
    }

    /**
     * 转小写
     */
    private static char charToLowerCase(char ch) {
        if (ch <= 90 && ch >= 65) {
            ch += 32;
        }
        return ch;
    }

    /**
     * 将string格式化保留两位
     * @return
     */
    public static String formatTwoDecimal(String object){
        if(object == null || object == ""){
            return object;
        }
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format(new BigDecimal(object));
    }

    /**
     * 将id字符串转换为集合
     */
    public static Set<Integer> convertIntegerSet(String ids) {
        Set<Integer> idList = new HashSet<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ids)) {
            String[] roleArr = ids.split("[,\\.，。 、]");
            for (String roleId : roleArr) {
                if (org.apache.commons.lang3.StringUtils.isBlank(roleId)) {
                    continue;
                }
                try {
                    idList.add(Integer.parseInt(roleId));
                } catch (NumberFormatException e) {
                    // id异常，掠过
                }
            }
        }
        return idList;
    }
    public static List<Integer> convertIntegerList(String ids) {
        List<Integer> idList = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ids)) {
            String[] roleArr = ids.split("[,\\.，。 、]");
            for (String roleId : roleArr) {
                if (org.apache.commons.lang3.StringUtils.isBlank(roleId)) {
                    continue;
                }
                try {
                    idList.add(Integer.parseInt(roleId));
                } catch (NumberFormatException e) {
                    // id异常，掠过
                }
            }
        }
        return idList;
    }
    public static Set<Long> convertLongSet(String ids) {
        Set<Long> idList = new HashSet<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ids)) {
            String[] roleArr = ids.split("[,\\.，。 、]");
            for (String roleId : roleArr) {
                if (org.apache.commons.lang3.StringUtils.isBlank(roleId)) {
                    continue;
                }
                try {
                    idList.add(Long.parseLong(roleId));
                } catch (NumberFormatException e) {
                    // id异常，掠过
                }
            }
        }
        return idList;
    }
    public static List<Long> convertLongList(String ids) {
        List<Long> idList = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ids)) {
            String[] roleArr = ids.split("[,\\.，。 、]");
            for (String roleId : roleArr) {
                if (org.apache.commons.lang3.StringUtils.isBlank(roleId)) {
                    continue;
                }
                try {
                    idList.add(Long.parseLong(roleId));
                } catch (NumberFormatException e) {
                    // id异常，掠过
                }
            }
        }
        return idList;
    }

    public static List<String> convertStringList(String ids) {
        List<String> idList = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ids)) {
            String[] roleArr = ids.split("[,\\.，。 、]");
            for (String roleId : roleArr) {
                if (org.apache.commons.lang3.StringUtils.isBlank(roleId)) {
                    continue;
                }
                idList.add(roleId);
            }
        }
        return idList;
    }


    public static List<String> convertStringListBy(String ids) {
        List<String> idList = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ids)) {
            String[] roleArr = ids.split("[;]");
            for (String roleId : roleArr) {
                if (org.apache.commons.lang3.StringUtils.isBlank(roleId)) {
                    continue;
                }
                idList.add(roleId);
            }
        }
        return idList;
    }

}
